var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var communityUrl = require("url/community");
var communityJoinUrl = communityUrl.url.join.joinList;
var helperHttp = require("helper/http");
var root = ui("$");
//本页面-ajax业务请求url
var httpToUpdateUrl = communityUrl.url.notice.toUpdate;

//本页面-ajax业务请求url
var httpUpdateUrl = communityUrl.url.notice.update;

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");

//加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
var imgMax = 3;
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "编辑公告",
	"labelNext" : "确认"
})
header.refreshData();
/*
 ***** 输入的文本框
 */
var layout_main = ui('do_LinearLayout_main');
var addboxContent = layout_main.add("boxContent", "source://view/reuse/form/mutiTextarea.ui",0,0);
var boxContent = ui("boxContent");
boxContent.bindData(hashdata);
var textBoxContent = ui(addboxContent + ".textArea");

hashdata.addData({
    "textAreaHint":'公告内容'
});
boxContent.refreshData();
/*
 ***** 选择社区
 */
var labelCommunityId = '';
var addcommunityName = layout_main.add("communityName", "source://view/reuse/form/mutiLabel.ui",0,0);
var communityName = ui("communityName");
communityName.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "选择社区",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
communityName.refreshData();
var communityNameText = ui(addcommunityName+".labelText");
var addPicker = aLayoutRoot.add("picker","source://view/reuse/form/oneCategory.ui",0,0);
var picker = ui("picker");
ui(addcommunityName+".labelAout").on("touch","",300,function(){
	do_Page.hideKeyboard();
	var params={}
	var ptitle={"title":"选择社区","id":"cId"}
	helperHttp.post(communityJoinUrl, params, function(data) {
		var pcount = [ptitle,data.communityList];
		do_Page.fire("oneCategoryList",pcount);
		do_Page.fire("oneCategory","true");
	});
});
do_Page.on("refreshOneCategory",function(data){
	if("cId" == data[1]){
		communityNameText.text = data[0].onename;
		labelCommunityId = data[0].oneid;
	}
});
var type=1,id;
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	//读取当前页面的传入参数
	var data=do_Page.getData();
	id=data.id;
	loadPage();
});
function loadPage(){
	var params={type:type,id:id};
	helperHttp.post(httpToUpdateUrl,params,function(data){
		if(data!=null){
			textBoxContent.text=data.communityNewsVo.content;
			labelCommunityId=data.communityNewsVo.communityId;
			communityNameText.text = data.communityNewsVo.communityName;
		}
	});
	
}

headerRight.on("touch", function() {
	update();
})
function update() {
	do_Page.hideKeyboard();
	if (textBoxContent.text.trim() == '' || textBoxContent.text.trim() == null) {
		nf.toast("亲，请写点什么吧。");
		return;
	}
	if (labelCommunityId == '' || labelCommunityId == null) {
		nf.toast("亲，请选择社区。");
		return;
	}
	var params={type:type,content:textBoxContent.text,communityId:labelCommunityId,id:id};
	helperHttp.post(httpUpdateUrl,params,function(data){
		nf.toast("编辑成功");
		do_App.closePage();
	});
}
//点击背景隐藏输入键盘
root.on("touch", function() {
	do_Page.hideKeyboard();
})